import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:osc/utils/pd_util.dart';
import 'package:osc/utils/string_util.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';

class ImageLoader extends StatelessWidget {
  final String url;
  final String errorImageName;
  final String placeholderImageName;
  final BoxFit fit;
  final double width;
  final double height;
  final FilterQuality filterQuality;
  const ImageLoader({Key key, @required this.url, this.errorImageName = "ic_split_graph", this.fit = BoxFit.cover, this.placeholderImageName, this.width, this.height, this.filterQuality = FilterQuality.low}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      width: width,
      height: height,
      imageUrl: PdUtil.getFullUrl(url),
      placeholder: (context, url){
        return _loader();
      },
      errorWidget:(context, url, error){
        return _error();
      },
      fit: fit,
      filterQuality: filterQuality,
      cacheManager: DefaultCacheManager(),
    );
  }

  _loader() {
    return new Center(
      child: StringUtil.isEmpty(placeholderImageName)?CircularProgressIndicator():Image.asset(PdUtil.getImagePath(placeholderImageName)),
    );
  }

  _error() {
    return new Center(
      child: Image.asset(PdUtil.getImagePath(errorImageName)),
    );
  }
}
